package com.longtop.perform.zb.index.web;

import java.util.BitSet;

import org.apache.commons.lang.StringUtils;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.security.BaseCheckedException;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;
import com.longtop.perform.zb.index.model.TZbIndex;
import com.longtop.perform.zb.index.service.IndexCustomService;

/**
 * 复合指标--新增或修改
 * 
 * @author 薛延明
 */
public class IndexCustomEditAction extends BaseAction {

	private final static String DATASET_INDEX = "Dataset_Index";
	private IndexCustomService indexCustomService = getBean(IndexCustomService.BEAN_ID);

	/**
	 * 系统共用：初始化调用
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {

		Dataset dataset = context.getDataset(DATASET_INDEX);
		if (!WebUtil.validateDatasetExist(this, dataset, DATASET_INDEX)) {
			return;
		}

		String indexId = this.getParameter(context, "id");
		TZbIndex zbIndex = null;
		// 判断是新增或编辑
		if (StringUtils.isNotEmpty(indexId)) {
			try {
				zbIndex = indexCustomService.findZbIndexInfoByIndexId(indexId);
			} catch (BaseCheckedException e) {
				this.hintErrorMessage(e.getMessage());
			} catch (Exception e) {
				e.printStackTrace();
			}
			dataset.appendObject(zbIndex);
		}
	}

	/**
	 * 保存指标
	 * 
	 * @param context
	 */
	public void saveIndexCustomInfo(IActionContext context) {

		String editType = context.getCommandParameterValue("editType");
		Dataset dataset = (Dataset) context.getDataset(DATASET_INDEX);
		if (!WebUtil.validateDatasetChange(this, dataset, DATASET_INDEX)) {
			return;
		}
		try {
			TZbIndex zbIndex = BeanUtil.datasetToVO(dataset, TZbIndex.class);
			// 取得用户对象
			SessionObject sessionObject = this.getSessionObject(context);

			// 公式转换与校验
			BitSet bitSet = new BitSet("010000000000".length());
			// String sqlExp =
			// Translator.getInstance().translate(zbIndex.getFormula(), bitSet,
			// "", "", sessionObject.getOrgId());
			// zbIndex.setFormulaSql(sqlExp);

			if (editType == null || editType.equals("0")) {
				// 设置创建者信息
				zbIndex.setCreateOrgId(sessionObject.getOrgId());
				zbIndex.setCreateOrgName(sessionObject.getOrgName());
				zbIndex.setCreateTime(sessionObject.getLoginTime());
				zbIndex.setCreateUserId(sessionObject.getUserId());
				zbIndex.setCreateUserName(sessionObject.getUserName());
				// 保存复合指标信息
				indexCustomService.saveIndexCustomInfo(zbIndex);
			} else if ("1".equals(editType)) {
				// 设置修改者信息
				zbIndex.setUpdateTime(sessionObject.getLoginTime());
				zbIndex.setUpdateUserName(sessionObject.getUserName());
				// 修改复合指标信息
				indexCustomService.updateIndexCustomInfo(zbIndex);
			}
		} catch (BaseCheckedException e) {
			this.hintErrorMessage(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}